package com.sky.controller.admin;



import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealController {


    @Autowired
    SetMealService setMealService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealcache",key="#setmealDTO.categoryId")
    public Result insert(@RequestBody SetmealDTO setmealDTO){
        setMealService.insert(setmealDTO);
        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    @GetMapping("/{id}")
    @ApiOperation("id查询套餐")
    public Result<SetmealDTO> getById(@PathVariable Long id){
        SetmealDTO setmealDTO = setMealService.getById(id);
        return Result.success(setmealDTO);
    }

    @PutMapping
    @ApiOperation("套餐修改")
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("状态更改")
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        setMealService.stopOrStart(status,id);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("套餐删除")
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        setMealService.deleteByIds(ids);
        return Result.success();
    }

}
